#!/usr/bin/env python2
# coding:utf-8

import sys
from time import sleep
from multiprocessing import Process
import importlib

import ftpserver
import jaka_grpc_server

# import jaka_tcp_server
# import jaka_check
import pylog

importlib.reload(sys)
# sys.setdefaultencoding('utf-8')  # 只能在python2上运行


# ftp
def FtpStart():
    process_ftp = Process(target=ftpserver.start, name="ftpserver")
    process_ftp.start()
    return process_ftp


# grpc50051 & udp54545
def GrpcStart():
    process_grpc = Process(target=jaka_grpc_server.GrpcServerStart, name="jaka_grpc_server")
    process_grpc.start()
    return process_grpc


# tcp 10000 10001 10003 10004
def TcpStart():
    process_tcp = Process(target=jaka_tcp_server.Tcp_server_start, name="jaka_tcp_server")
    process_tcp.start()
    return process_tcp


# def AddOnControllerStart():
#     process_addon_controller = Process(target=addon_controller_start,)
#     process_addon_controller.start()


def CheckStart():
    process_check = Process(target=jaka_check.check_start, name="jakacheck")
    process_check.start()
    return process_check


if __name__ == "__main__":
    pylog_logger = pylog.pylog_logger

    pylog.setup()
    pylog_logger().debug("----------------------------------[NEW STRUCT OF PROGRAM]----------------------------------")

    # AddOnControllerStart()
    obj_dir = [
        GrpcStart(),
        FtpStart(),
        # TcpStart(),
        # CheckStart(),
    ]

    try:
        while True:
            for process in obj_dir:
                pylog_logger().debug("pid: {}, name: {}".format(process.pid, process.name))
            sleep(24 * 60 * 60)
    except KeyboardInterrupt:
        for process in obj_dir:
            name = process.name
            pid = process.pid
            process.terminate()
            process.join()
            pylog_logger().debug("{}({}) exited !".format(name, pid))
        pass
